$(function (){
    loadCategories();
});

function loadCategories(){
    $("#software_category_list").empty();
    $.get("/index/tool/categories", {}, res => {
        let categoriesDomStr = '';
        if(res.status == 0 && res.data){
            res.data.map(category => {
                categoriesDomStr += '<div class="main-list">'
                categoriesDomStr += '<h1>' + category.name + '</h1>';
                categoriesDomStr += '<ul>';
                category.softwareList.map(software => {
                    if(software.versions && software.versions.length > 1){
                        categoriesDomStr += '<li class="second">';
                        categoriesDomStr += '<div class="dl-row center">';
                        categoriesDomStr += '<img src="/local/images/tlogo.png" alt="">';
                        categoriesDomStr += '<div class="name dl-column start">';
                        categoriesDomStr += '<div class="">';
                        categoriesDomStr += '<h5>' + software.name + '</h5>';
                        categoriesDomStr += '</div>';
                        categoriesDomStr += '<p>' + software.description + '</p>';
                        categoriesDomStr += '</div>';
                        categoriesDomStr += '<div>';
                        categoriesDomStr += '<div className="down-box dropdown-toggle" id="dropdownMenuButton"';
                        categoriesDomStr += 'data-toggle="dropdown" aria-expanded="false">';
                        categoriesDomStr += '<img class="down" src="/local/images/icon/down-c.svg" alt="">';
                        categoriesDomStr += '</div>';
                        categoriesDomStr += '<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">';
                        software.versions.map(version => {
                            categoriesDomStr += '<a class="dropdown-item" href="' + version.address + '" target="_blank">' + version.name + '</a>';
                        });
                        categoriesDomStr += '</div>';
                        categoriesDomStr += '</div>';
                        categoriesDomStr += '</div>';
                        categoriesDomStr += '</li>';
                    }else{
                        categoriesDomStr += '<li class="">';
                        categoriesDomStr += '<a href="' + software.versions[0].address + '" target="_blank">';
                        categoriesDomStr += '<div class="dl-row center">';
                        categoriesDomStr += '<img src="/local/images/tlogo.png" alt="">';
                        categoriesDomStr += '<div class="name dl-column start">';
                        categoriesDomStr += '<div class="">';
                        categoriesDomStr += '<h5>' + software.name + '</h5>';
                        categoriesDomStr += '</div>';
                        categoriesDomStr += '<p>' + software.description + '</p>';
                        categoriesDomStr += '</div>';
                        categoriesDomStr += '</div>';
                        categoriesDomStr += '</a>';
                        categoriesDomStr += '</li>';
                    }
                });
                categoriesDomStr += '</ul>';
                categoriesDomStr += '</div>'
            });
        }
        $("#software_category_list").html(categoriesDomStr);
    });
}